<?php
    /**
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/9/14
     * Time: 10:40
     */

    namespace app\api\controller;


    use app\lib\exception\UploadException;

    class Upload extends BaseController
    {
        /**
         * 图片上传
         * @return array
         * @throws UploadException
         */
        public function upload()
        {
            $file = input('file.uploadfile');
            // 移动到框架应用根目录/uploads/ 目录下
            $info = $file->validate(['size' => 20000000, 'ext' => 'jpg,png,gif,jpeg'])->move('uploads');

            if ($info) {
                // 成功上传后 获取上传信息
                $rootPath     = '/uploads/' . date('Ymd', time());
                $fullFilePath = request()->domain() . $rootPath . '/' . $info->getFilename();
                $saveFilePath = $rootPath . '/' . $info->getFilename();
                $result       = ['fullFilePath' => $fullFilePath, 'saveFilePath' => $saveFilePath];
                return $result;
            } else {
                // 上传失败获取错误信息
                throw new UploadException(['msg' => $file->getError()]);
            }
        }


    }